﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.OleDb;
using Models;
using Lohus.Model;
using System.Web.Security;

namespace Lohus.Controllers
{
    public class CaseController : Controller
    {
        //
        // GET: /Case/

        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList(string categoryID)
        {
            string sql = @"SELECT Article.[ID], Article.[CategoryID], Article.[ArticleName], Article.[Content], Article.[CreateTime],tc.ClassName
                                FROM Article LEFT JOIN casecategories tc on article.CategoryID = tc.ID  where tc.id=" + categoryID;
            List<ArticleExt> result = new List<ArticleExt>();
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                con.Open();
                OleDbCommand cmd = new OleDbCommand(sql, con);
                using (OleDbDataReader dr = cmd.ExecuteReader())
                {
                    if (dr != null)
                    {
                        while (dr.Read())
                        {
                            ArticleExt article = new ArticleExt();
                            article.ID = Convert.ToInt32(dr["ID"].ToString());
                            article.CategoryID = Convert.ToInt32(dr["CategoryID"].ToString());
                            article.Title = dr["ArticleName"].ToString();
                            article.Content = dr["Content"].ToString();
                            article.CreateTime = Convert.ToDateTime(dr["CreateTime"].ToString());
                            //article.Order = Convert.ToInt32(dr["Order"].ToString());
                            article.CategoryName = dr["ClassName"].ToString();
                            result.Add(article);
                        }
                    }
                }
                ViewData["result"] = result;
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取所有的案例分类
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCaseCategoryList()
        {
            string sql = "select * from caseCategories";
            List<CaseCategories> result = new List<CaseCategories>();
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                con.Open();
                OleDbCommand cmd = new OleDbCommand(sql, con);
                using (OleDbDataReader dr = cmd.ExecuteReader())
                {

                    if (dr != null)
                    {
                        while (dr.Read())
                        {
                            CaseCategories category = new CaseCategories();
                            category.ID = Convert.ToInt32(dr["ID"].ToString());
                            category.ClassName = dr["ClassName"].ToString();
                            category.Code = dr["Code"].ToString();
                            category.Img = dr["Img"].ToString();
                            category.Remark = dr["Remark"].ToString();
                            category.Color = dr["Color"].ToString();
                            result.Add(category);
                        }
                    }
                }
                ViewData["result"] = result;
            }
            result.Reverse();
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取分类图片
        /// </summary>
        /// <param name="categoryID"></param>
        /// <returns></returns>
        public ActionResult GetClassPhoto(string categoryID)
        {
            //string sql = "select top 5 * from casePhoto c where c.classid=" + categoryID + " order by c.Order";
            string sql = "select top 5 id,classid,imgpath from casePhoto c where c.classid=" + categoryID;
            List<CasePhoto> result = new List<CasePhoto>();
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                con.Open();
                OleDbCommand cmd = new OleDbCommand(sql, con);
                using (OleDbDataReader dr = cmd.ExecuteReader())
                {
                    if (dr != null)
                    {
                        while (dr.Read())
                        {
                            CasePhoto cphoto = new CasePhoto();
                            cphoto.ID = Convert.ToInt32(dr["ID"].ToString());
                            cphoto.ClassID = Convert.ToInt32(dr["ClassID"].ToString());
                            cphoto.ImgPath = dr["ImgPath"].ToString();
                            result.Add(cphoto);
                        }
                    }
                }
                ViewData["result"] = result;
            }
            result.Reverse();
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 查看文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult PreviewArticle(string id)
        {

            id = id.Trim('\'');
            string sql = @"SELECT   Article.ID, Article.CategoryID, Article.ArticleName, Article.Content, Article.CreateTime, Article.[Order], 
                           caseCategories.classname
                           FROM      (Article LEFT OUTER JOIN
                           CaseCategories ON Article.CategoryID = CaseCategories.ID) where Article.id=" + id;

            ArticleExt article = new ArticleExt();
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                con.Open();
                OleDbCommand cmd = new OleDbCommand(sql, con);
                using (OleDbDataReader dr = cmd.ExecuteReader())
                {
                    if (dr != null)
                    {
                        while (dr.Read())
                        {
                            ViewData["ID"] = Convert.ToInt32(dr["ID"].ToString());
                            ViewData["Title"] = dr["ArticleName"].ToString();
                            ViewData["Content"] = dr["Content"].ToString();
                            ViewData["CreateTime"] = Convert.ToDateTime(dr["CreateTime"].ToString());
                            ViewData["CategoryName"] = dr["ClassName"].ToString();
                        }
                    }
                }
            }
            return View();
        }
        [ValidateInput(false)]
        public ActionResult AddArticle(string title, int classID, string Content, string ID)
        {

            if (FormsAuthentication.GetAuthCookie("user", false) == null)
            {
                throw new Exception("无访问权限！");
            }
            if (string.IsNullOrEmpty(title))
            {
                return Json(0, JsonRequestBehavior.AllowGet);
            }
            string sql = "";
            if (!string.IsNullOrEmpty(ID))
            {
                sql = "update article set categoryID =" + classID + ",articleName='" + title + "',Content='" + Content + "' where id = " + ID;
            }
            else
            {
                sql = "insert into article(categoryID,articleName,Content,CreateTime) values({0},'{1}','{2}','{3}')";
                sql = string.Format(sql, classID, title, Content, DateTime.Now);
            }
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                try
                {
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(sql, con);
                    int i = cmd.ExecuteNonQuery();
                    return Json(i, JsonRequestBehavior.AllowGet);
                }
                catch
                {
                    return Json(0, JsonRequestBehavior.AllowGet);
                }

            }

        }

        /// <summary>
        /// 查看所有案例文章列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ShowList()
        {
            return View();
        }

        public ActionResult GetArticleList()
        {
            string sql = @"SELECT Article.[ID],  Article.[ArticleName], Article.[CreateTime],tc.ClassName as CNAME
                                FROM Article LEFT JOIN casecategories tc on article.CategoryID = tc.ID";
            List<ArticleExt> result = new List<ArticleExt>();
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                con.Open();
                OleDbCommand cmd = new OleDbCommand(sql, con);
                using (OleDbDataReader dr = cmd.ExecuteReader())
                {
                    if (dr != null)
                    {
                        while (dr.Read())
                        {
                            ArticleExt article = new ArticleExt();
                            article.ID = Convert.ToInt32(dr["ID"].ToString());
                            article.Title = dr["ArticleName"].ToString();
                            article.CreateTime = Convert.ToDateTime(dr["CreateTime"].ToString());
                            article.CategoryName = dr["CNAME"].ToString();
                            result.Add(article);
                        }
                    }
                }
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 删除案例文章
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public ActionResult DeleteArticle(string ID)
        {

            if (FormsAuthentication.GetAuthCookie("user", false) == null)
            {
                throw new Exception("无访问权限！");
            }
            string sql = "delete from article where id =" + ID;
            using (OleDbConnection con = new OleDbConnection(DBHelper.DBHelper.connectionString))
            {
                try
                {
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(sql, con);
                    return Json(cmd.ExecuteNonQuery() > 0, JsonRequestBehavior.AllowGet);
                }
                catch
                {
                    return Json(false, JsonRequestBehavior.AllowGet);
                }
            }
        }
    }
}
